package org.smart4j.chapter1.controller;

import org.smart4j.chapter1.service.CustomerService;
import org.smart4j.chapter1.utils.CastUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 删除客户
 *
 * Created by LG93 on 2017/8/10.
 */
@WebServlet("/delete")
public class CustomerDeleteServlet extends HttpServlet {

	private CustomerService customerService;

	@Override
	public void init() throws ServletException {
		customerService = new CustomerService();
	}

	/**
	 * 删除客户
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		long id = CastUtil.castLong(request.getParameter("id"));
		boolean flag = customerService.deleteCustomer(id);
		request.setAttribute("flag", flag ? "删除成功" : "删除失败");
		request.getRequestDispatcher("/WEB-INF/view/customer_delete.jsp").forward(request, response);

    }
}
